import os

class FolderManager:
    def __init__(self, folder_prefix, target_directory):
        self.folder_prefix = folder_prefix
        self.target_directory = target_directory

    def get_next_folder_number(self):
        existing_folders = [f for f in os.listdir(self.target_directory) if os.path.isdir(os.path.join(self.target_directory, f)) and f.startswith(self.folder_prefix)]
        if not existing_folders:
            return 1
        max_folder_number = max([int(f[len(self.folder_prefix):]) for f in existing_folders])
        return max_folder_number + 1

    def create_new_folder(self):
        new_folder_number = self.get_next_folder_number()
        new_folder_name = f"{self.folder_prefix}{new_folder_number}"
        new_folder_path = os.path.join(self.target_directory, new_folder_name)
        os.makedirs(new_folder_path)
        return new_folder_path


import os


class FileNameGenerator:
    def __init__(self, base_path, base_name):
        self.base_path = base_path
        self.base_name = base_name

    def get_new_file_name(self):
        # 获取路径下所有的文件名称
        files = [f for f in os.listdir(self.base_path) if os.path.isfile(os.path.join(self.base_path, f))]

        # 过滤出符合base_name和数字编号格式的文件，并提取编号
        file_numbers = []
        for file in files:
            if file.startswith(self.base_name):
                try:
                    # 获取文件名中的编号部分，例如 "file_1.txt" 提取出 "1"
                    number = int(file[len(self.base_name):].split('.')[0])
                    file_numbers.append(number)
                except ValueError:
                    continue

        # 找到最大的编号并加1作为新的编号
        if file_numbers:
            new_number = max(file_numbers) + 1
        else:
            new_number = 1

        # 构造新的文件名
        new_file_name = f"{self.base_name}{new_number}"
        return new_file_name




if __name__ == "__main__":
    # target_directory = "..//result"  # Replace this with the desired target directory
    # folder_manager = FolderManager("image", target_directory)
    # new_folder_path = folder_manager.create_new_folder()
    # print(f"新建文件夹：{new_folder_path}")
    # 使用示例
    base_path = 'F:\\Project\\force_pi\\ur5-robot-master\\result\\demotraj'  # 替换为你的文件路径
    base_name = 'csv'  # 替换为你的基础文件名

    generator = FileNameGenerator(base_path, base_name)
    new_file_name = generator.get_new_file_name()
    print(f"新的文件名: {new_file_name}")
